package com.fastloan.mgt.invest.controller.temp;

import com.fastloan.mgt.invest.model.InvestSubject;
import com.fastloan.mgt.invest.model.InvestTemp;
import com.fastloan.mgt.invest.model.InvestType;
import com.fastloan.mgt.invest.service.InvestSubjectService;
import com.fastloan.mgt.invest.service.InvestTempService;
import com.fastloan.mgt.invest.service.InvestTypeService;
import com.fastloan.safe.core.util.ManagerKit;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 模版管理
 *
 * @author rankai
 *         createTime 2017-08-2017/8/8 10:40
 */
@RestController
@RequestMapping("/api/invest/temp")
public class InvestTempController extends BaseController<InvestTemp> {

    @Autowired
    private InvestTempService investTempService;

    @Autowired
    private InvestTypeService investTypeService;

    @Autowired
    private InvestSubjectService investSubjectService;

    /**
     * 模版分页列表
     *
     * @return
     */
    @RequestMapping("/pageList")
    public ResultTo pageList(HttpServletRequest request) {
        Example example = new Example(InvestTemp.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("topOrgId", ManagerKit.getTopOrgId().toString())
                .andNotEqualTo("state", 2);
        String state = request.getParameter("state");
        String title = request.getParameter("title");
        if (StringUtils.isNotBlank(state)) {
            criteria.andEqualTo("state", state);
        }
        if (StringUtils.isNotBlank(title)) {
            criteria.andLike("title", title);
        }
        return dataPage(example);
    }

    /**
     * 模版
     *
     * @param orgId 机构ID
     * @return
     */
    @GetMapping("/add/{orgId}")
    public ResultTo selectAll(@PathVariable("orgId") String orgId) {
        return new ResultTo().setData(investTempService.selectByOrgId(orgId));
    }

    /**
     * 获取增加模版表单数据所需信息
     *
     * @param investTempId 机构ID
     * @return
     */
    @GetMapping("/addVar/{investTempId}")
    public ResultTo selectForAdd(@PathVariable("investTempId") String investTempId) {
        return new ResultTo().setData(investTempService.selectForAdd(investTempId));
    }

    /**
     * 根据ID获取模版
     *
     * @param investTempId 模版ID
     * @return
     */
    @GetMapping("/{investTempId}")
    public ResultTo select(@PathVariable(value = "investTempId") String investTempId) {
        InvestTemp investTemp = investTempService.selectByPrimaryKey(investTempId);
        if (investTemp.getState() == 2) {
            return new ResultTo(600, "该项已删除");
        }
        Map<String, Object> map = new HashMap<>();
        InvestType investType = new InvestType();
        investType.setTopOrgId(ManagerKit.getTopOrgId().toString());
        investType.setState(1);
        map.put("investType", investTypeService.select(investType));
        Example example = new Example(InvestSubject.class);
        example.createCriteria().andEqualTo("state", 1);
        map.put("subjects", investSubjectService.selectByExample(example));
        map.put("investTemp", investTemp);
        map.put("tempOrg", investTempService.selectForOrg(investTempId));
        return new ResultTo().setData(map);
    }

    /**
     * 增加模版
     *
     * @param investTemp 模版bean
     * @return
     */
    @PostMapping
    public ResultTo insert(InvestTemp investTemp) {
        investTemp.setTopOrgId(ManagerKit.getTopOrgId().toString());
        investTemp.setCreateTime(new Date());
        investTemp.setState(3);
        investTempService.insertSelective(investTemp);
        return new ResultTo().setData(investTemp.getId());
    }

    /**
     * 修改模版
     *
     * @param investTemp 模版bean
     * @param orgIds     机构ID,ID之间用','隔开
     * @return
     */
    @PutMapping
    public ResultTo update(InvestTemp investTemp,
                           @RequestParam(value = "orgIds", required = false) String orgIds) {
        investTemp.setTopOrgId(null);
        investTempService.updateForOrg(investTemp, orgIds);
        return new ResultTo();
    }

    /**
     * 发布模版
     *
     * @param tempId 模版ID
     * @param orgIds 机构ID数组
     * @return
     */
    @PutMapping("/{tempId}")
    public ResultTo release(@PathVariable(value = "tempId") String tempId,
                            @RequestParam("orgIds") String orgIds) {
        InvestTemp investTemp = investTempService.selectByPrimaryKey(tempId);
        if (investTemp.getState() == 2) {
            return new ResultTo(600, "该模版已被删除");
        }
        if (StringUtils.isBlank(orgIds)) {
            return new ResultTo(ResultEnum.PARAM_ERROR);
        }
        investTempService.release(investTemp, orgIds);
        return new ResultTo();
    }

    /**
     * 模版名称验重
     *
     * @param name 　模版名称
     * @return
     */
    @GetMapping(params = "name")
    public ResultTo haveName(@RequestParam("name") String name) {
        Example example = new Example(InvestTemp.class);
        example.createCriteria().andNotEqualTo("state", 2)
                .andEqualTo("topOrgId", ManagerKit.getTopOrgId().toString())
                .andEqualTo("title", name);
        if (investTempService.selectByExample(example).size() > 0) {
            return new ResultTo().setData(false);
        } else {
            return new ResultTo().setData(true);
        }
    }
}
